/*
 * GDO GeometryFactory
 * 
 * initial implementation : 31.8.2007
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;

namespace Petra.Gis.DataServices.GDO
{
  public unsafe partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates linestringGeometry
     * from intergraph blob structure
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    private static GeometryBase binaryToLinestring(byte[] vData, int arrayOffset, System.Type parentGeomType)
    {
      CoordinateList Coords;
      LineString oGeom = new LineString();
      //int pocet;
      //if input geometry is line then there are only 2 points fixed
      Guid ingrGUID;
      fixed (byte* bGUID = &vData[arrayOffset])
      {
        ingrGUID = (*((Guid*)bGUID));
      }

      if (ingrGUID == getGuid[GeometryType.Line])
      {
        Coords = new CoordinateList(vData, arrayOffset + 16, 2, Dimension.dim3D);
      }
      else
      {
        fixed (byte* bCoordListPtr = &vData[arrayOffset + 16])
        {
          int* lNumPoints = (Int32*)bCoordListPtr;
          //pocet = *lNumPoints;
          Coords = new CoordinateList(vData, arrayOffset + 20, *lNumPoints, Dimension.dim3D);
        }
      }
      oGeom.coordList = Coords;

      return oGeom;
    }




    /***********************************************
     * 28.8.2007
     * 
     * creates intergraph blob structure 
     * from linestringGeometry
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static byte[] linestringToBinary(GeometryBase geometry)
    {
      int lBlobSize;
      Guid gGeomGUID;

      LineString _linestring = (LineString)geometry;

      lBlobSize = 16 + 4 + (_linestring.coordList.Count * 24);
      gGeomGUID =  getGuid[GeometryType.Polyline];

      System.IO.MemoryStream oIMStream = new System.IO.MemoryStream(lBlobSize);
      System.IO.BinaryWriter mBWriter = new System.IO.BinaryWriter(oIMStream);

      oIMStream.Position = 0;
      mBWriter.Write(gGeomGUID.ToByteArray());
      //todo:ensure int32 for 64bit platform !!!
      mBWriter.Write((Int32)_linestring.coordList.Count);
      mBWriter.Write(_linestring.coordList.ToArray(Dimension.dim3D));

      return oIMStream.ToArray();
    }
  }
}
